<?php

namespace App\Http\Controllers\Mini;

use App\Models\ElectricPaymentOrder;
use App\Models\User;
use App\Models\Users;
use App\Services\BillService;
use App\Services\CostService;
use \Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

/**
 * 账单
 * Class Bill
 * @package app\api\controller
 * @author sdd
 */
class BillController extends BaseController
{
    public $ownerId = "";
    public $user;

    public function __construct()
    {
        DB::connection("mysql_center")->enableQueryLog();
        $this->user = auth("user")->user();
        $this->ownerId = 102;
//        $this->ownerId=auth("user")->id();
    }

    /**
     * @return mixed
     */
    public function index()
    {
        $totalCumulativeConsumption = '';//累计消费
        $electricCumulativeConsumption = '';//累计消费
        $waterCumulativeConsumption = '';//累计消费
        $propertyCumulativeConsumption = '';//累计消费
        $data = [
            'totalCumulativeConsumption' => $totalCumulativeConsumption,
            'electricCumulativeConsumption' => $electricCumulativeConsumption,
            'waterCumulativeConsumption' => $waterCumulativeConsumption,
            'propertyCumulativeConsumption' => $propertyCumulativeConsumption,
        ];
        return $this->success('返回成功', $data);
    }

    /**
     * 账单列表
     * @return array
     * @author sdd
     */
    public function getBillList(): array
    {

        if (!$this->ownerId) {
            return $this->error("请登录");
        }
        $owner = Users::select(Db::raw('electric_id,id'))->find($this->ownerId);
        if (!$owner) {
            return $this->error('用户不存在');
        }
        $date_old = request('date', '');
        $dateArr = explode('.', $date_old);
        $date = $dateArr[0] . '-' . $dateArr[1];
        if (!$date_old) {
            return $this->error('请传递日期');
        }
        $type = \request('type', 1);
        $res = (new BillService)->getBillList($date, $owner['electric_id'], $type);
        return $res ? $this->success($res, "获取成功") : $this->error("暂无数据");
    }

    /**
     * 订单详情
     * @author sdd
     */
    public function bill_details(): array
    {
        $order_id = \request('order_id', '');
        if (!$order_id) {
            return $this->error('请传递订单id');
        }
        $data = ElectricPaymentOrder::where(['id' => $order_id, "user_id" => $this->ownerId])->first();
        if (!$data) {
            return $this->error('暂无订单');
        }
        $data = $data->toArray();
        return $data ? $this->success($data, "获取成功") : $this->error("暂无订单");

    }

    /**
     *  *电费/水费
     * @return array
     * @author sdd
     */
    public function costDetails(): array
    {
        $param['type'] = \request('type', 0);
        if (!in_array($param['type'], [1, 2])) {
            return $this->error('费用类型有误，请传递1或2！');
        }
        $owner = Users::select(Db::raw("elc_code,water_code,community"))->find($this->ownerId);
        $code = "";
        switch ($param['type']) {
            case 1:
                $code = $owner['elc_code'];
                break;
            case 2:
                $code = $owner['water_code'];
                break;
            default:
                break;
        }
        $param["code"] = $code;
        if (!$param['type']) {
            return $this->error('请传递费用类型');
        }
//        if (!$param['code']) {
//            return $this->error('您还没有绑定设备，请扫码进行绑定');
//        }
        $res = (new BillService)->getDetails($param, $owner);
        $res['meter_code'] = $code;//电表或水表资产编号
        return $res ? $this->success($res, "获取成功") : $this->error("暂无信息");
    }

    /**
     *    *物业费账单
     * @return array
     * @author sdd
     */
    public function getPropertyBill(): array
    {
        $owner = Users::select(Db::raw('elc_code,water_code,community,property_plan_id,id'))->find($this->ownerId);
        if (!$owner) {
            return $this->error(['code' => 2, 'msg' => '请登录']);
        }
        if (!$owner['property_plan_id']) {
            return $this->error('未绑定物业费收费方案');
        }
        $year = \request("year", "");
        if (!$year) {
            return $this->error('请选择年份');
        }
        $res = (new BillService)->getPropertyLog($year, $owner);
        return $res ? $this->success($res, "获取成功") : $this->error("暂无数据");
    }

    /**
     *物业费预存明细
     * @author sdd
     */
    public function preDepositDetail(): array
    {
        $owner = auth("user")->user();
        if (!$owner) {
            return $this->error(['code' => 2, 'msg' => '请登录']);
        }

        $months = \request('months', "");
        if (!$months) {
            return $this->error('请选择预存时间');
        }
        $res = (new BillService)->getProperty($months, $owner);
        return $res ? $this->success($res, "获取成功") : $this->error("暂无数据");
    }

    /**
     *缴费记录
     * @return array
     * @author sdd
     */
    public function payBillList(): array
    {
        if (!$this->ownerId) {
            return $this->error(['code' => 2, 'msg' => '请登录']);
        }
        $param = \request()->all();
        $res = (new BillService)->payBillList($this->ownerId, $param);
        return $res ? $this->success($res, "获取成功") : $this->error("暂无缴费记录");
    }

    /**
     * 获取总账单 电费 水费 物业费
     * @author sdd
     */
    public function getBillInfo(): array
    {
        $this->ownerId = auth("user")->id();
        if (!$this->ownerId) {
            return $this->error(['code' => 2, 'msg' => '请登录']);
        }
        $res = BillService::billInfo($this->ownerId);
        return $res ? $this->success($res, "获取成功") : $this->error("暂无数据");
    }


    /**
     * 获取缴费页面的信息
     * @author sdd
     */
    public function getBillDetail(): array
    {
        $owner = auth("user")->user();
        if (!$owner) {
            $get = \request()->all();
            $user_id = $get['user_id'];
        } else {
            $user_id = $owner['id'];
        }
        $owner = Users::find($user_id);
        if (!$owner) {
            return $this->error(['code' => 2, 'msg' => '请登录']);
        }
        $res = (new BillService)->getBillDetail($owner);
        return $res ? $this->success($res, "获取成功") : $this->error("暂无数据");
    }

    /**
     *周月年
     * @author  sdd
     */
    public function energyAnalysis(): array
    {
        if (!$this->ownerId) {
            return $this->error(['code' => 2, 'msg' => '请登录']);
        }
        $owner = $owner = Users::find($this->ownerId);
        $param = request()->all();
        if (!$param['type'] || !$param['code'] || !$param['cate']) {
            return $this->error('缺少参数');
        }
        $res = [];
        switch ($param['cate']) {
            case "week":
                $res = (new BillService())->getWeekEnergyAnalysis($param, $owner);
                break;
            case "months":
                $res = (new BillService())->getMonthEnergyAnalysis($param, $owner);
                break;
            case 'year':
                $res = (new BillService())->getYearEnergyAnalysis($param, $owner);
                break;
            default:
                break;
        }
        return $this->success($res, '获取成功');
    }
}
